package com.bailan.foreat.controller;


import com.bailan.foreat.entity.Comment;
import com.bailan.foreat.entity.Item;
import com.bailan.foreat.entity.Product;
import com.bailan.foreat.entity.User;
import com.bailan.foreat.service.CartService;
import com.bailan.foreat.service.ItemService;
import com.bailan.foreat.service.OrderService;
import com.bailan.foreat.service.ProductService;
import com.bailan.foreat.utils.UserUtils;
import com.bailan.foreat.vo.CartVo;
import com.bailan.foreat.vo.ItemVo;
import com.bailan.foreat.vo.OrderVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ste
 * @since 2023-02-21 07:50:43
 */
@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    OrderService orderService;

    @Autowired
    UserController userController;

    @Autowired
    ProductService productService;

    @Autowired
    ItemService itemService;

    @Autowired
    CartService cartService;

    @RequestMapping("allItems")
    @ResponseBody
    public CartVo allItems(@RequestParam("productIds[]") Integer[] productIds, HttpServletRequest request){
        //获取用户信息
        User currentUser = userController.currentUser(request);
        //从购物车里获取详情
        CartVo cartVo = orderService.getCartVo(currentUser.getId(), productIds);
        return cartVo;
    }


    @RequestMapping("createOrder")
    @ResponseBody
    public String createOrder(@RequestParam("productIds[]") Integer[] productIds, Integer addressId,HttpServletRequest request){
        //lock
        synchronized ("abc"){

            //获取当前用户信息
            User currentUser = UserUtils.getCurrentUser(request);

            //判断购物车是否为空
            if (!cartService.containProductIds(productIds, currentUser.getId())) {
                return "订单已提交，请勿重复提交订单!";
            }

            //判断商品库存
            for (Integer productId : productIds) {
                Product product = productService.selectById(productId);
                if(product.getStockCount()<1){
                    System.out.println("█商品 "+product.getProductName()+" 的库存量不足");
                    return "商品 "+product.getProductName()+" 的库存量不足！";
                }
            }

            try{
                orderService.createOrder(currentUser,productIds,addressId);
            }catch (RuntimeException e){
                return e.getMessage();
            }
            return "ok";

        }

    }

    @RequestMapping("updateOrderStatus")
    @ResponseBody
    public String updateOrderStatus(Integer orderId,Integer newStatus){
        return orderService.updateOrderStatus(orderId,newStatus);
    }

    @RequestMapping("receivingOrderItem")
    @ResponseBody
    public void receivingOrderItem(Integer orderId, Integer storeId, Integer newStatus) throws IOException {
        orderService.receivingOrderItem(orderId,storeId,newStatus);
    }

    @RequestMapping("refundOrderItem")
    @ResponseBody
    public String refundOrderItem(Integer orderId,Integer storeId, Integer newStatus, String refundReason){
        System.out.println("orderId: "+orderId+" storeId: "+storeId+" newStatus: "+newStatus+" refundReason: "+refundReason);
        if("".equals(refundReason)){
            return "请填写退款原因！";
        }
        orderService.refundOrderItem(orderId,storeId,newStatus,refundReason);
        return "ok";
    }

    @RequestMapping("checkOrderItem")
    @ResponseBody
    public String checkOrderItem(@RequestBody List<ItemVo> itemVos){
        return orderService.checkOrderItem(itemVos.get(0).getId());
    }

    @RequestMapping("reviewOrderItem")
    @ResponseBody
    public String reviewOrderItem(@RequestBody List<Comment> comments,HttpServletRequest request){
        User user = userController.currentUser(request);
        System.out.println("comments: "+printArrayList(comments));
        return orderService.reviewOrderItem(comments,user.getId());
    }

    @RequestMapping("queryAllOrder")
    @ResponseBody
    public Page<OrderVo> queryAllOrder(@RequestParam(defaultValue = "1") Integer currentPage,
                                         @RequestParam(defaultValue = "3") Integer pageSize,
                                         @RequestParam(defaultValue = "") String keyword,
                                         HttpServletRequest request){
        User currentUser = userController.currentUser(request);
        Page<OrderVo> orderVoPage = orderService.queryAllOrder(currentPage, pageSize, keyword,currentUser);
//        System.out.println("████████████  orderVoPage.getRecords()"+orderVoPage.getRecords());
        return orderVoPage;
    }

    public String printArrayList(List list){
        String listStr = "[";
        for (Object o : list) {
            if(o instanceof List){
                printArrayList((List)o);
            }else {
                listStr += o.toString();
            }
        }
        listStr+="]";
        return listStr;
    }
}

